using UnityEngine;
using System.Collections;

public class enemyRangeTrigger : MonoBehaviour {
	
	public GameObject rotationPart;
	public Color c1 = Color.yellow;
    public Color c2 = Color.red;
    public int lengthOfLineRenderer = 2;
	
	private LineRenderer lineRenderer;
	private LineRenderer lineRenderer2;
	private PlayerWControl script;

	// Use this for initialization
	void Start () {
		lineRenderer = rotationPart.transform.FindChild("firepoint1").gameObject.AddComponent<LineRenderer>();
		lineRenderer2 = rotationPart.transform.FindChild("firepoint2").gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        lineRenderer.SetColors(c1, c2);
		lineRenderer.useWorldSpace = true;
        lineRenderer.SetWidth(0.03F, 0.03F);
        lineRenderer.SetVertexCount(lengthOfLineRenderer);
		lineRenderer2.material = new Material(Shader.Find("Particles/Additive"));
        lineRenderer2.SetColors(c1, c2);
		lineRenderer2.useWorldSpace = true;
        lineRenderer2.SetWidth(0.03F, 0.03F);
        lineRenderer2.SetVertexCount(lengthOfLineRenderer);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnTriggerEnter(Collider collider) {
		if (collider.gameObject.tag == "Player") {
			audio.Play();	
			lineRenderer.enabled = true;
			lineRenderer2.enabled = true;
		}
	}
	
	void OnTriggerExit(Collider collider) {
		if (collider.gameObject.tag == "Player") {
			audio.Stop();	
			lineRenderer.enabled = false;	
			lineRenderer2.enabled = false;	
		}
	}
	
	void OnTriggerStay (Collider collider) {
		if (collider.gameObject.tag == "Player") {
			Vector3 v = Vector3.RotateTowards(rotationPart.transform.forward, collider.transform.position - rotationPart.transform.position, 100f * Time.deltaTime, 0.0f);
			rotationPart.transform.rotation = Quaternion.LookRotation(v);
			//rotationPart.transform.position = Vector3.MoveTowards(transform.position, player.transform.position, step*Time.deltaTime);
			//Debug.DrawRay(rotationPart.transform.position, v, Color.red);
			lineRenderer.SetPosition(0, rotationPart.transform.FindChild("firepoint1").transform.position);
			lineRenderer.SetPosition(1, collider.transform.position);
			lineRenderer2.SetPosition(0, rotationPart.transform.FindChild("firepoint2").transform.position);
			lineRenderer2.SetPosition(1, collider.transform.position);
			script = collider.GetComponent<PlayerWControl>();
			script.decreaseHealth(0.05f);
		} 
	}
}
